package hanelsoft.vn.dictionaylearn.control.activity;

import hanelsoft.vn.dictionaylearn.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Setting extends Activity{
	CheckBox save_history, play_sound;
	SharedPreferences shareSaveHistory, sharePlaySound;
	TextView setting_time_wait_of_widget_time;
	LinearLayout setting_time_wait_of_widget;
	SharedPreferences shareWidget;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.setting);
		shareWidget = getSharedPreferences("ShareWidget", 0);
		save_history = (CheckBox)findViewById(R.id.setting_save_history);
		play_sound = (CheckBox)findViewById(R.id.setting_play_sound);
		setting_time_wait_of_widget_time = (TextView)findViewById(R.id.setting_time_wait_of_widget_time);
		setting_time_wait_of_widget = (LinearLayout)findViewById(R.id.setting_time_wait_of_widget);
		String timeold = shareWidget.getInt("Setting_Time_Wait_Widget", 0)+getString(R.string._seconds);
		setting_time_wait_of_widget_time.setText(timeold);
		shareSaveHistory = getSharedPreferences("CBsavehistory", 1);
		sharePlaySound = getSharedPreferences("CBplaysound", 1);
		if(shareSaveHistory.getInt("savehistory", 0) == 1)
		{
			save_history.setChecked(true);
		}
		else
		{
			save_history.setChecked(false);
		}
		save_history.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(save_history.isChecked())
				{
					shareSaveHistory.edit().putInt("savehistory", 1).commit();
				}
				else
				{
					shareSaveHistory.edit().putInt("savehistory", 0).commit();
				}
			}
		});
		if(sharePlaySound.getInt("playsound", 0) == 1)
		{
			play_sound.setChecked(true);
		}
		else
		{
			play_sound.setChecked(false);
		}
		play_sound.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(play_sound.isChecked())
				{
					sharePlaySound.edit().putInt("playsound", 1).commit();
				}
				else
				{
					sharePlaySound.edit().putInt("playsound", 0).commit();
				}
			}
		});
		setting_time_wait_of_widget.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialogSetTimeWidget();
			}
		});
	}
	
	private void showDialogSetTimeWidget()
	{
		LayoutInflater mLayoutInflater = LayoutInflater.from(this);
		View mView = mLayoutInflater.inflate(R.layout.setting_dialog_timewidget, null);
		final EditText timewait = (EditText)mView.findViewById(R.id.setting_dialog_timewidget_time);
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
		alertDialogBuilder.setView(mView);
		alertDialogBuilder.setTitle(getText(R.string.time_wait_of_widget));
		alertDialogBuilder.setIcon(R.drawable.setting);
		alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                finish();
            }
        }).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
			@Override
            public void onClick(DialogInterface dialog, int which) {
				int time = Integer.parseInt(timewait.getText().toString());
				if(time == 0)
				{
					time = 5;
				}
				setting_time_wait_of_widget_time.setText(time+" "+getString(R.string._seconds));
				shareWidget.edit().putInt("Setting_Time_Wait_Widget", time).commit();
            }
        }).setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
    	Dialog dialog = alertDialogBuilder.create();
        dialog.show();
	}
}

